﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.EarTage;
using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.EarsCommands
{
    public class LambEarSearchHander : IRequestHandler<LambEarSearchCommand, ResultT<LambEarListDto>>
    {

        private readonly ILogger<LambEarSearchHander> logger;
        private readonly IBaseRepository<SleepInfor> sleepInfoRepository;
        private readonly IBaseRepository<SleepType> sleepTypeRepository;
        private readonly IBaseRepository<LambEars> lambEarRepository;
        private readonly IBaseRepository<LambBirthdayStatus> lambBirStatusRepository;
        private readonly IBaseRepository<LambBreed> lambBreedRepository;
        private readonly IBaseRepository<Employee> employeeRepository;

        public LambEarSearchHander(ILogger<LambEarSearchHander> logger, IBaseRepository<SleepInfor> sleepInfoRepository, IBaseRepository<SleepType> sleepTypeRepository, IBaseRepository<LambEars> lambEarRepository, IBaseRepository<LambBirthdayStatus> lambBirStatusRepository, IBaseRepository<LambBreed> lambBreedRepository, IBaseRepository<Employee> employeeRepository)
        {
            this.logger = logger;
            this.sleepInfoRepository = sleepInfoRepository;
            this.sleepTypeRepository = sleepTypeRepository;
            this.lambEarRepository = lambEarRepository;
            this.lambBirStatusRepository = lambBirStatusRepository;
            this.lambBreedRepository = lambBreedRepository;
            this.employeeRepository = employeeRepository;
        }
        /// <summary>
        /// 查询产羔耳标管理信息
        /// </summary>
        /// <param name="request">查询产羔耳标管理信息请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>查询产羔耳标管理信息响应对象</returns>
        public async Task<ResultT<LambEarListDto>> Handle(LambEarSearchCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultT<LambEarListDto>();
                var list = (from a in lambEarRepository.GetAll().Where(c=>!c.Deletes)
                            join b in lambBirStatusRepository.GetAll() on a.BrithdayStatusId equals b.BrithdayStatusId
                            join c in lambBreedRepository.GetAll() on a.BreedId equals c.BreedId
                            join f in employeeRepository.GetAll() on a.CreateId equals f.EmployeeId
                            select new LambEarListDto
                            {
                                LambEarId = a.LambEarId,
                                EarNumber = a.EarNumber,
                                PutInDate = a.PutInDate,
                                BreedId = a.BreedId,
                                BreedName = c.BreedName,
                                BrithdayStatusId = a.BrithdayStatusId,
                                BrithdayStatus = b.BrithdayStatus,
                                UseStatus = a.UseStatus,
                                CreateName = f.FirstName,
                            });
                if (!string.IsNullOrEmpty(request.EarNumber))
                {
                    list = list.Where(c => c.EarNumber.Contains(request.EarNumber));
                }
                if (request.BreedId != 0)
                {
                    list = list.Where(c => c.BreedId == request.BreedId);
                }
                if (request.BrithdayStatusId != 0)
                {
                    if (request.BrithdayStatusId == 1)
                    {
                        list = list.Where(c => c.BrithdayStatusId <= 3);
                    }
                    else
                    {
                        list = list.Where(c => c.BrithdayStatusId > 3);
                    }

                }
                if (request.UseStatus >-1)
                {
                    list = list.Where(c => c.UseStatus == Convert.ToBoolean( request.UseStatus));
                }
                result.Data = list.ToList();
                logger.LogInformation("查询产羔耳标管理信息方法调用成功");
                return result;

            }
            catch (Exception ex)
            {
                logger.LogError("获取产羔耳标管理列表信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
